﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GH.BLL;
using System.Data;
using GH.Model;
using GH.Web;


public partial class VipMemberCardAdd : GH.Web.BasePage.PageBase
{
    public string UionID = "";
    public string DepartmentID = "";
    public string GroupID = "";

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.IsPostBack)
        {

            //文化程度
            Common.SetDataBound(ddlCuture, "101");
            //政治面貌
            Common.SetDataBound(ddlPro, "102");
            //职称
            Common.SetDataBound(ddllever, "103");
            //属性
            Common.SetDataBound(ddlProperty, "104");
            DataBindDdlUnit();
            GH.Model.会员基本信息 model = new GH.Model.会员基本信息();
            if (Request.QueryString["ID"] != null)
            {
                hfID.Value = Request.QueryString["ID"].ToString();
                VipMember bllVipMember = new VipMember();
                model = bllVipMember.GetModel(int.Parse(Request.QueryString["ID"].ToString()));
                //ID								
                txtIdenfication.Text = model.身份证号;
                txtName.Text = model.姓名;
                hfName.Value = model.姓名;
                ddlSex.SelectedValue = model.性别;
                txtBornDate.Text = string.Format("{0:s}", model.出生年月日).Substring(0, 10);
                txtNation.Text = model.籍贯;
                txtIncome.Text = model.收入;
                txtTel.Text = model.联系电话;
                txtAddress.Text = model.家庭住址;
                ddlCuture.SelectedValue = model.文化程度;
                ddlPro.SelectedValue = model.政治面貌;
                this.ddlUion.SelectedValue = model.单位.ToString();
                DataBindDdlDepartment();
                DataBindDdlGroup();
                this.ddlDepartment.SelectedValue = model.部门.ToString();
                this.ddlGroup.SelectedValue = model.班组.ToString();
                ddllever.SelectedValue = model.职称;
                txtCoWorkTime.Text = model.连续工龄;
                txtWorkTime.Text = model.本企业工龄;
                txtHate.Text = model.有害人体健康工作;
                rdIsMember.SelectedValue = model.会员区分;
                txtMemberNo.Text = model.会员证号;
                txtInMemberTime.Text = string.Format("{0:s}", model.入会时间).Substring(0, 10);
                ddlProperty.SelectedValue = model.属性;
                txtReason.Text = model.调进调出原因;
                txtWorkHistory.Text = model.工作经历;
                txtRemarks.Text = model.备注;
                this.txtJobTime.Text = string.Format("{0:s}", model.参加工作时间).Substring(0,10);
            }
            else
            {
                DataBindDdlDepartment();
                DataBindDdlGroup();
            }



        }

    }

    void DataBindDdlUnit()
    {
        GH.BLL.Union unitBLL = new Union();
        System.Text.StringBuilder strWhere = new System.Text.StringBuilder();
        //操作其他单位会员权限
        if (!this.GetRight(201))
        {
            strWhere.AppendFormat(" 工会单位信息.ID={0}", int.Parse(Session["Union"].ToString()));
        }
        else
        {
            strWhere.AppendFormat("");
        }
        DataTable dtQuery = unitBLL.GetList(strWhere.ToString()).Tables[0];
        this.ddlUion.DataSource = dtQuery;
        this.ddlUion.DataTextField = "单位名称";
        this.ddlUion.DataValueField = "ID";
        this.ddlUion.DataBind();
    }

    void DataBindDdlDepartment()
    {
        int unitID = Convert.ToInt32(this.ddlUion.SelectedValue);

        this.ddlDepartment.Items.Clear();
        GH.BLL.DepartMent DepartMentBLL = new DepartMent();
        this.ddlDepartment.DataSource = DepartMentBLL.GetModelList("单位ID=" + unitID);
        this.ddlDepartment.DataTextField = "部门名称";
        this.ddlDepartment.DataValueField = "ID";
        this.ddlDepartment.DataBind();
        ddlDepartment.Items.Insert(0, new ListItem(" ", "-1"));
        ddlDepartment.SelectedValue = "-1";

    }

    void DataBindDdlGroup()
    {
        int unitID = Convert.ToInt32(this.ddlUion.SelectedValue);

        this.ddlGroup.Items.Clear();
        GH.BLL.Group GroupBLL = new Group();
        this.ddlGroup.DataSource = GroupBLL.GetModelList("单位ID=" + unitID);
        this.ddlGroup.DataTextField = "班组名称";
        this.ddlGroup.DataValueField = "ID";
        this.ddlGroup.DataBind();
        ddlGroup.Items.Insert(0, new ListItem(" ", "-1"));
        ddlGroup.SelectedValue = "-1";

    }

    protected void btnAdd_Click(object sender, ImageClickEventArgs e)
    {
        if (this.ddlGroup.SelectedValue.ToString() == "-1" && this.ddlDepartment.SelectedValue.ToString() == "-1")
        {
            AlertMessage("部门或者班组请至少选择一项!");
            this.ddlDepartment.Focus();
            return;
        }

        if (this.rdIsMember.SelectedValue == "1" && this.txtMemberNo.Text.Trim() == "")
        {
            AlertMessage("请输入会员编号！");
            this.txtMemberNo.Focus();
            return;

        }
        try
        {
            bool IsEdit = false;
            if (Request.QueryString["ID"] != null)
            {
                IsEdit = true;
            }
            VipMember bllVipMember = new VipMember();
            GH.Model.会员基本信息 model = new GH.Model.会员基本信息();

            if (IsEdit)
            {
                model = bllVipMember.GetModel(int.Parse(Request.QueryString["ID"].ToString()));
            }
            //model.ID		 ID						
            model.身份证号 = txtIdenfication.Text;
            model.姓名 = txtName.Text;
            model.性别 = ddlSex.SelectedValue;
            model.出生年月日 = System.DateTime.Parse(txtBornDate.Text);
            model.籍贯 = txtNation.Text;
            model.收入 = txtIncome.Text;
            model.联系电话 = txtTel.Text;
            model.家庭住址 = txtAddress.Text;
            model.文化程度 = ddlCuture.SelectedValue;
            model.政治面貌 = ddlPro.SelectedValue;

            model.单位 = int.Parse(Request.Form["ddlUion"]);
            model.部门 = int.Parse(Request.Form["ddlDepartment"]);
            model.班组 = int.Parse(Request.Form["ddlGroup"]);
            model.职称 = ddllever.SelectedValue;
            model.连续工龄 = txtCoWorkTime.Text;
            model.本企业工龄 = txtWorkTime.Text;
            model.有害人体健康工作 = txtHate.Text;
            model.会员区分 = rdIsMember.Text;
            model.会员证号 = txtMemberNo.Text;
            model.入会时间 = System.DateTime.Parse(txtInMemberTime.Text);
            model.属性 = ddlProperty.SelectedValue;
            model.调进调出原因 = txtReason.Text;
            model.工作经历 = txtWorkHistory.Text;
            model.备注 = txtRemarks.Text;
            model.参加工作时间 = System.DateTime.Parse(this.txtJobTime.Text);


            if (IsEdit)
            {
                bllVipMember.Update(model);
            }
            else
            {
                bllVipMember.Add(model);
            }
            AlertMessage("更新成功");
        }
        catch
        {
            AlertMessage("更新失败");
        }

    }

    protected void btnFamily_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Redirect("VipFamilyList.aspx");
    }

    protected void ddlUion_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBindDdlDepartment();
        DataBindDdlGroup();
    }
}
